package tcdx.uap.common.utils;

import java.text.SimpleDateFormat;
import java.util.*;

public class Lutils {

    public static Map[] MapListToArray(List<Map> list) {
        Map[] array = new Map[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
        return array;
    }

    public static List getColumnValueList(List<Map> l1, String columnName){
        List valueList = new ArrayList<Object>();
        for(Map node:l1){
            valueList.add(node.get(columnName));
        }
        return valueList;
    }
    public static Map findItemInList(List<Map> l1,String field,String value){
        List valueList = new ArrayList<Object>();
        for(Map item:l1){
            if(item.containsKey(field)&&item.get(field).toString().equals(value))
                return item;
        }
        return null;
    }

    public static boolean isItemInList(List l1,int value){
        for(Object item:l1){
            if(Integer.parseInt(item.toString())==value)
                return true;
        }
        return false;
    }

    public static boolean isItemInList(List l1,String value){
        for(Object item:l1){
            if(item.toString().equals(value))
                return true;
        }
        return false;
    }

    public static Map findItemInList(List<Map> l1,String field,int value){
        List valueList = new ArrayList<Object>();
        for(Map item:l1){
            if(item.containsKey(field)&&Integer.parseInt(item.get(field).toString())==value)
                return item;
        }
        return null;
    }

    public static List<Map> findListInList(List<Map> l1,String field,int value){
        List<Map> valueList = new ArrayList<Map>();
        for(Map item:l1){
            if(item.containsKey(field)&&Integer.parseInt(item.get(field).toString())==value)
                valueList.add(item);
        }
        return valueList;
    }

    public static boolean safe_sql(String sql_seg){
        if(sql_seg.length()>63)
            return false;
        return sql_seg.matches("([A-Za-z0-9]|[\\u4e00-\\u9fa5]|_|\\.)+");
    }
    public static boolean safe_sql_list(List l){
        for(Object sql_seg:l){
            if(!safe_sql(sql_seg.toString()))
                return false;
        }
        return true;
    }


    public static boolean safe_sql_map(Map<String, Object> map){
        if(map==null)
            return true;
        for (Map.Entry<String, Object> entry : map.entrySet())
        {
            if(!safe_sql(entry.getKey().toString()))
                return false;
        }
        return true;
    }
    public static void insertToTree(Map p, Map node,String parentIdColumnName,String nodeIdColumnName,String childrenColumnName){
        //若是找到父节点
        if(node.get(parentIdColumnName).equals(p.get(nodeIdColumnName))){
            if(!p.containsKey(childrenColumnName)) p.put(childrenColumnName, new ArrayList());
            ((List)p.get(childrenColumnName)).add(node);
        }
        else{
            if(p.get(childrenColumnName)==null) return ;
            for(Map subP: ((List<Map>)p.get(childrenColumnName))){
                insertToTree(subP, node, parentIdColumnName, nodeIdColumnName,childrenColumnName);
            }
        }
    }
    //从叶子，往上创建树
    public static void buildTree(List<Map<String,Object>> children, Map root,String parentIdColumnName,String nodeIdColumnName,String childrenColumnName) {
        children = SortMapArrayByField(children,false,"order_id");
        List <Map> waitDeletedChild = new ArrayList<Map>();
        //在roots找到node的孩子节点，将孩子节点
        for (Map child : children) {
            if (child.get(parentIdColumnName).equals(root.get(nodeIdColumnName))) {
                //构建子树
                if(root.get(childrenColumnName)==null)
                    root.put(childrenColumnName, new ArrayList<Map>());
                ((List)root.get(childrenColumnName)).add(child);
                //从根集中，删除子节点
                waitDeletedChild.add(child);
            }
        }
        for(Map delc: waitDeletedChild)
            children.remove(delc);
        if( !root.get("tp").equals("folder") || root.get(childrenColumnName)!=null)
            children.add(root);
    }


    public static List<Map<String,Object>> SortMapArrayByField(List<Map<String,Object>> List,boolean type,String field){
        //排序
        Collections.sort(List, new Comparator<Map<String,Object>>() {
            @Override
            public int compare(Map<String,Object> o1, Map<String,Object> o2) {
                // 进行排序
                if (Integer.parseInt(String.valueOf(o1.get(field))) > Integer.parseInt(String.valueOf(o2.get(field)))) {
                    return 1;
                }
                if (Integer.parseInt(String.valueOf(o1.get(field))) == Integer.parseInt(String.valueOf(o2.get(field)))) {
                    return 0;
                }
                return -1;
            }
        });
        //降序
        if(type){
            Collections.reverse(List);
        }
        return List;
    }
    //快速生成Map
    public static Map<String,Object> genMap(Object...args){
        Map <String,Object> gm = new HashMap<String,Object>();
        for(int i=0;i<args.length;i+=2){
            gm.put(args[i].toString(),args[i+1]);
        }
        return gm;
    }
    //快速生成List
    public static List<String> genList(String...args){
        List<String> re= new ArrayList<String>();
        for(int i=0;i<args.length;i++){
            re.add(args[i]);
        }
        return re;
    }

    //快速生成List
    public static List<Object> genList(Object...args){
        List<Object> re= new ArrayList<Object>();
        for(int i=0;i<args.length;i++){
            re.add(args[i]);
        }
        return re;
    }

    //快速生成List
    public static List<Integer> genList(Integer...args){
        List<Integer> re= new ArrayList<Integer>();
        for(int i=0;i<args.length;i++){
            re.add(args[i]);
        }
        return re;
    }

    public static List<Map> genList(Map...args){
        List<Map> re= new ArrayList<Map>();
        for(int i=0;i<args.length;i++){
            re.add(args[i]);
        }
        System.out.println(re.toString());
        return re;
    }

    public static List<List> genList(List<Map>...args){
        List re= new ArrayList<List<HashMap>>();
        for(int i=0;i<args.length;i++){
            re.add(args[i]);
        }
        System.out.println(re.toString());
        return re;
    }
    //快速生成List
    public static List<Long> genList(Long...args){
        List<Long> re= new ArrayList<Long>();
        for(int i=0;i<args.length;i++){
            re.add(args[i]);
        }
        return re;
    }
    public static Map<String,Object> copyMap(Map<String,Object> mp){
        HashMap <String,Object> gm = new HashMap<String,Object>();
        gm.putAll(mp);
        return gm;
    }

    public static String getTime(){
        Date nowTime=new Date();
        SimpleDateFormat time=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return time.format(nowTime);
    }

    public static String nvl(String o,String a){
        if(o==null)
            return a;
        return o;
    }

    public static int nvl(Object o,int a){
        if(o==null)
            return a;
        return Integer.parseInt(o.toString());
    }

    public static boolean nvl(Object o,boolean a){
        if(o==null)
            return a;
        return Boolean.parseBoolean(o.toString());
    }


    public static final int JOIN_VALUE_TYPE_INT = 1;
    public static final int JOIN_VALUE_TYPE_STR = 2;
    public static List<Map> leftJoin(List<Map> left,List<Map> right,String left_on,String right_on,int valueType,String []putCols){
        List<Map> re = new ArrayList<Map>();
        for(Map le:left){
            Map em = Lutils.copyMap(le);
            Map find = null;
            if(valueType==JOIN_VALUE_TYPE_INT)
                find = Lutils.findItemInList(right,right_on,Integer.parseInt(le.get(left_on).toString()));
            else if(valueType==JOIN_VALUE_TYPE_STR)
                find = Lutils.findItemInList(right,right_on,le.get(left_on).toString());
            if(find!=null) {
                //更新字段值
                for (String col : putCols) {
                    em.put(col, find.get(col));
                }
            }
            re.add(em);
        }
        return re;
    }

    public static List<Map> leftJoin(List<Map> left,List<Map> right,String left_on,String right_on,int valueType,Map<String,Object> putCols){
        List<Map> re = new ArrayList<Map>();
        for(Map le:left){
            Map em = Lutils.copyMap(le);
            Map find = null;
            if(valueType==JOIN_VALUE_TYPE_INT)
                find = Lutils.findItemInList(right,right_on,Integer.parseInt(le.get(left_on).toString()));
            else if(valueType==JOIN_VALUE_TYPE_STR)
                find = Lutils.findItemInList(right,right_on,le.get(left_on).toString());
            if(find!=null) {
                //更新字段值
                for (Map.Entry<String, Object> entry : putCols.entrySet())
                {
                    em.put(entry.getValue(), find.get(entry.getKey()));
                }
            }
            re.add(em);
        }
        return re;
    }

    public static void listAddCol(List<Map> l, String colName, Object defaultVal){
        for(Map le:l){
            le.put(colName, defaultVal);
        }
    }

    public static boolean isInList(List<Object> l, int v){
        for(Object o:l){
            if(Integer.parseInt(o.toString())==v)
                return true;
        }
        return false;
    }

    public static boolean isInList(List<Object> l, String v){
        for(Object o:l){
            if(o.toString().equals(v))
                return true;
        }
        return false;
    }

    public static List<Map> makeTree(List<Map> datas,int startNodeParentId,int link_id) {
        List<Map> trees = new ArrayList<>();
        for (Map data : datas) {
            if ( startNodeParentId == Integer.parseInt(data.get("parent_org_id").toString())
                    && link_id== Integer.parseInt(data.get("link_id").toString())) {
                trees.add(data);
                data.put( "children",makeTree( datas, Integer.parseInt(data.get("id").toString()) ,link_id));
            }
        }
        return trees;
    }

    public static List<Map> makeTree2(List<Map> datas,String startNodeParentId) {
        List<Map> trees = new ArrayList<>();
        for (Map data : datas) {
            if (data.get("parentid")!=null && startNodeParentId.equals(data.get("parentid").toString())) {
                data.put("label",data.get("topic"));
                data.put("value",data.get("id"));
                trees.add(data);
                data.put( "children",makeTree2( datas,data.get("id").toString()));
            }
        }
        return trees;
    }
}
